package com.gqz.equip.service.impl;

import cn.hutool.crypto.SmUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.EnWzEquipBqEntity;
import com.gqz.equip.app.entity.TEnWzEquipEntity;
import com.gqz.equip.app.mapper.TEnWzEquipBqMapper;
import com.gqz.equip.app.mapper.TEnWzEquipMapper;
import com.gqz.equip.app.mapstruct.TEnWzEquipBqMapstruct;
import com.gqz.equip.app.service.MqTEnWzEquipService;
import com.gqz.equip.service.ITEnWzEquipBqService;
import com.gqz.equip.vo.TEnWzEquipBqVo;
import com.gqz.qrcode.util.QRCodeUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 新能源-物资-出入库工单表 业务逻辑实现层ServiceImpl
 *
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class TEnWzEquipBqServiceImpl extends ServiceImpl<TEnWzEquipBqMapper, EnWzEquipBqEntity> implements ITEnWzEquipBqService {//
    private String key = "SGemsBJ-1234!@#$";
    private String prefix = "SM4(";
    private String subfix = ")";

    @Resource
    private TEnWzEquipBqMapstruct mapstruct;
    @Resource
    private TEnWzEquipMapper tEnWzEquipMapper;

    @Resource
    private MqTEnWzEquipService mqTEnWzEquipService;


    @Override
    public List<TEnWzEquipBqVo> getById(String id) {
        MPJLambdaWrapper<EnWzEquipBqEntity> queryWrapper = new MPJLambdaWrapper<>();
        /* todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.eq(EnWzEquipBqEntity::getId, id);
        queryWrapper.selectAll(EnWzEquipBqEntity.class);
        List<EnWzEquipBqEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }


    @Override
    public Long count(RequestForm<TEnWzEquipBqVo> requestForm) {
        EnWzEquipBqEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnWzEquipBqEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        /** todo getPsrId-> todo-must修改为记录表主键字段todo */
        queryWrapper.select(EnWzEquipBqEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<TEnWzEquipBqVo> vos) {
        List<EnWzEquipBqEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzEquipBqEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<TEnWzEquipBqVo> vos) {
        List<EnWzEquipBqEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnWzEquipBqEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    @Override
    public List<TEnWzEquipBqVo> getListEwm(TEnWzEquipBqVo requestForm) {

        MPJLambdaWrapper<TEnWzEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzEquipEntity.class);
        queryWrapper.select(EnWzEquipBqEntity::getId);
        queryWrapper.selectAs(EnWzEquipBqEntity::getTqrName, "tqr");
        queryWrapper.select(EnWzEquipBqEntity::getTqrId);
        queryWrapper.select(EnWzEquipBqEntity::getTqTime);
        queryWrapper.select(EnWzEquipBqEntity::getQjzp);
        queryWrapper.select(EnWzEquipBqEntity::getEwmzp);
        queryWrapper.select(EnWzEquipBqEntity::getShztmc);
        queryWrapper.select(EnWzEquipBqEntity::getShztbm);
        queryWrapper.select(EnWzEquipBqEntity::getShyj);
        queryWrapper.select(EnWzEquipBqEntity::getShPerson);
        queryWrapper.select(EnWzEquipBqEntity::getShTime);
        queryWrapper.selectAs(EnWzEquipBqEntity::getIsTq, "sftq");
        queryWrapper.leftJoin(EnWzEquipBqEntity.class, EnWzEquipBqEntity::getSbbm, TEnWzEquipEntity::getSbbm);
        queryWrapper.eq(TEnWzEquipEntity::getSfsc, 0);
        queryWrapper.orderByDesc(EnWzEquipBqEntity::getIsTq);
        queryWrapper.orderByDesc(EnWzEquipBqEntity::getTqTime);
        if (requestForm.getSbbm() != null && !"".equals(requestForm.getSbbm())) {
            queryWrapper.like(TEnWzEquipEntity::getSbbm, requestForm.getSbbm());
        }
        if (requestForm.getCcbm() != null && !"".equals(requestForm.getCcbm())) {
            queryWrapper.like(TEnWzEquipEntity::getCcbm, requestForm.getCcbm());
        }
        if (requestForm.getSbgg() != null && !"".equals(requestForm.getSbgg())) {
            queryWrapper.like(TEnWzEquipEntity::getSbgg, requestForm.getSbgg());
        }
        if (requestForm.getSblxbm() != null && !"".equals(requestForm.getSblxbm())) {
            queryWrapper.eq(TEnWzEquipEntity::getSblxbm, requestForm.getSblxbm());
        }
        if (requestForm.getSbmc() != null && !"".equals(requestForm.getSbmc())) {
            queryWrapper.like(TEnWzEquipEntity::getSbmc, requestForm.getSbmc());
        }
        if (requestForm.getSftq() != null && !"".equals(requestForm.getSftq())) {
            queryWrapper.eq(EnWzEquipBqEntity::getIsTq, requestForm.getSftq());
        }
        if (requestForm.getShztbm() != null && !"".equals(requestForm.getShztbm())) {
            queryWrapper.eq(EnWzEquipBqEntity::getShztbm, requestForm.getShztbm());
        }

        if (requestForm.getTqTime() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.ge(EnWzEquipBqEntity::getTqTime, requestForm.getTqTime());
        }
        if (requestForm.getTqTime_to() != null) {
            //queryWrapper.ge(  :: getTestTime, Timestamp.valueOf(requestForm.getEntity().getTqTime()));
            queryWrapper.le(EnWzEquipBqEntity::getTqTime, requestForm.getTqTime_to());
        }
        List<TEnWzEquipBqVo> entities = tEnWzEquipMapper.selectJoinList(TEnWzEquipBqVo.class, queryWrapper);
        for (int i = 0; i < entities.size(); i++) {
            String sftq = entities.get(i).getSftq();
            if ("0".equals(sftq) || "".equals(sftq) || null == sftq) {
                entities.get(i).setSftq("否");
            } else {
                entities.get(i).setSftq("是");
            }
            if ("1".equals(entities.get(i).getEwm()) && entities.get(i).getSbbm() != null && entities.get(i).getCcbm() != null && entities.get(i).getSblxbm() != null && entities.get(i).getSbmc() != null) {
                //设备编码=1585954186420109314;出厂编码=HS-001-SNDJ-005;设备类型=100001;设备名称=设备名称;
                String content = "设备编码=" + entities.get(i).getSbbm() + ";出厂编码=" + entities.get(i).getCcbm() +
                        ";设备类型=" + entities.get(i).getSblxbm() + ";设备名称=" + entities.get(i).getSbmc();
                String encryptContent = SmUtil.sm4(key.getBytes()).encryptHex(content);
                String ewm = QRCodeUtil.getBase64QRCode(encryptContent, entities.get(i).getCcbm());
                entities.get(i).setEwm(ewm);
            } else {
                entities.get(i).setEwm("");
            }

        }
        if (CollectionUtils.isNotEmpty(entities)) {
            return entities;
        }
        return new ArrayList<>();
    }

    @Override
    public List<TEnWzEquipBqVo> getEwmBySbbm(String sbbm) {

        MPJLambdaWrapper<TEnWzEquipEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(TEnWzEquipEntity.class);
        queryWrapper.select(EnWzEquipBqEntity::getId);
        queryWrapper.selectAs(EnWzEquipBqEntity::getTqrName, "tqr");
        queryWrapper.select(EnWzEquipBqEntity::getTqrId);
        queryWrapper.select(EnWzEquipBqEntity::getTqTime);
        queryWrapper.select(EnWzEquipBqEntity::getQjzp);
        queryWrapper.select(EnWzEquipBqEntity::getEwmzp);
        queryWrapper.select(EnWzEquipBqEntity::getShztmc);
        queryWrapper.select(EnWzEquipBqEntity::getShztbm);
        queryWrapper.select(EnWzEquipBqEntity::getShyj);
        queryWrapper.select(EnWzEquipBqEntity::getShPerson);
        queryWrapper.select(EnWzEquipBqEntity::getShTime);
        queryWrapper.selectAs(EnWzEquipBqEntity::getIsTq, "sftq");
        queryWrapper.leftJoin(EnWzEquipBqEntity.class, EnWzEquipBqEntity::getSbbm, TEnWzEquipEntity::getSbbm);
        queryWrapper.eq(TEnWzEquipEntity::getSfsc, 0);
        queryWrapper.orderByDesc(EnWzEquipBqEntity::getIsTq);
        queryWrapper.orderByDesc(EnWzEquipBqEntity::getTqTime);
        queryWrapper.eq(TEnWzEquipEntity::getSbbm, sbbm);
        List<TEnWzEquipBqVo> entities = tEnWzEquipMapper.selectJoinList(TEnWzEquipBqVo.class, queryWrapper);
        for (int i = 0; i < entities.size(); i++) {
            String sftq = entities.get(i).getSftq();
            if ("0".equals(sftq) || "".equals(sftq) || null == sftq) {
                entities.get(i).setSftq("否");
            } else {
                entities.get(i).setSftq("是");
            }
            if (entities.get(i).getSbbm() != null && entities.get(i).getCcbm() != null && entities.get(i).getSblxbm() != null && entities.get(i).getSbmc() != null) {
                //设备编码=1585954186420109314;出厂编码=HS-001-SNDJ-005;设备类型=100001;设备名称=设备名称;
                String content = "设备编码=" + entities.get(i).getSbbm() + ";出厂编码=" + entities.get(i).getCcbm() +
                        ";设备类型=" + entities.get(i).getSblxbm() + ";设备名称=" + entities.get(i).getSbmc();
                String encryptContent = SmUtil.sm4(key.getBytes()).encryptHex(content);
                String ewm = QRCodeUtil.getBase64QRCode(encryptContent, entities.get(i).getCcbm());
                entities.get(i).setEwm(ewm);
            }

        }
        if (CollectionUtils.isNotEmpty(entities)) {
            return entities;
        }
        return new ArrayList<>();
    }


}
